package com.kaesar.spring.aop;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.time.StopWatch;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

/**
 * @author chengkai
 */
@Slf4j
@Aspect
@Component
public class TableRouteAspect {

    @Around("@annotation(tableRoute)")
    public Object doAround(ProceedingJoinPoint joinPoint, TableRoute tableRoute) throws Throwable {
        Object obj = null;

        StopWatch stopWatch = StopWatch.createStarted();
        try {
            log.info("[{}]开始执行...", tableRoute.name());

            Object[] args = joinPoint.getArgs();

            for (Object arg : args) {
                if (arg instanceof TableInfo) {
                    TableInfo tableInfo = (TableInfo) arg;
                    tableInfo.setTableName(tableRoute.name() + "_" + tableInfo.getPeriodYear() + "_" + tableInfo.getPeriodMonth());
                }
            }

            obj = joinPoint.proceed();
        } catch (Throwable var5) {
            log.error("[" + tableRoute.name() + "]执行失败!", var5);
            throw var5;
        }
        log.info("[{}]执行成功,耗时: {} ms", tableRoute.name(), stopWatch.getTime());

        return obj;
    }
}
